package com.zking.web;

import com.zking.dao.domain.Patient;
import com.zking.service.PatientService;
import com.zking.service.impl.PatientServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Tanglei
 * @date 2020/8/20 - 11:24
 */
@Controller
@RequestMapping(value = "/patient",name = "患者")
public class PatientController {

    @Autowired
    PatientService patientService;

    @RequiresPermissions("patient:toPatient")
    @RequestMapping(value = "/toPatient",name = "去病人页面")
    public String toPatient(Model model){
        //携带病人数据
        List<Patient> patients = patientService.queryPatients();
        System.out.println("patients = " + patients);
        model.addAttribute("patients",patients);
        return "backend_page/patients";
    }

    @RequestMapping(value = "/patientCount",name = "统计病患数量")
    @ResponseBody
    public Integer patientCount(){
        int count = patientService.queryPatientCount();
        return count;
    }

    @RequiresPermissions("patient:editPatient")
    @RequestMapping(value = "/editPatient",name = "修改患者")
    public String editPatient(Integer id,Model model){
        Patient patient = patientService.queryOnePatient(id);
        System.out.println("patient = " + patient);
        model.addAttribute("patient",patient);
        return "backend_page/edit-patient";
    }

    @RequiresPermissions("patient:updatePatient")
    @RequestMapping(value = "/updatePatient",name = "更新患者")
    public String updatePatient(Patient patient,Model model){
        Boolean b = patientService.updatePatient(patient);
        if (b){
            return "redirect:/patient/toPatient";
        }else {
            model.addAttribute("msg","修改失败");
            return "backend_page/edit-patient";
        }
    }
}
